<?php
/**
 * Created by PhpStorm.
 * Script Name: MpTag.php
 * Create: 2023/3/2 15:26
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\service;
use plugin\wechat\app\model\MpTag as TagM;
use support\Container;

class MpTag extends Mp
{
    /**
     * 标签删除
     * @param array $tag
     * @param null $mp_info
     * @return bool|false|mixed|string
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public static function tagDelete($tag = [], $mp_info = null)
    {
        try {
            $app = self::getApp($mp_info);
            $response = $app->user_tag->delete($tag['tag_id']);
            if(!empty($response['errcode'])){
                return $response['errmsg'];
            }
            TagM::where('id', $tag['id'])->delete();
        }catch (\Exception $e){
            return json_encode($e->getMessage(), JSON_UNESCAPED_UNICODE);
        }
        return true;
    }

    /**
     * 更新数据记录
     * @param array $tag
     * @param null $mp_info
     * @return \think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function syncDb($tag = [], $mp_info = null){
        empty($mp_info) && $mp_info = \request()->mpInfo;
        if($data = TagM::where('mp_id', $mp_info['id'])->where('tag_id', $tag['id'])->find()){
            $data['tag_id'] = $tag['id'];
            $tag['id'] = $data['id'];
            $res = TagM::update($tag);
        }else{
            $tag['tag_id'] = $tag['id'];
            $tag['mp_id'] = $mp_info['id'];
            $tag['company_id'] = $mp_info['company_id'];
            unset($tag['id']);
            $res = Container::make(TagM::class, [])->createOne($tag);
        }
        return $res;
    }

    /**
     * 更新标签
     * @param int $id
     * @param string $name
     * @param null $mp_info
     * @return array|\EasyWeChat\Kernel\Support\Collection|mixed|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public static function tagUpdate($id = 0, $name = '', $mp_info = null)
    {
        $app = self::getApp($mp_info);
        $response = $app->user_tag->update($id, $name);
        if(!empty($response['errcode'])){
            return $response['errmsg'];
        }
        return $response;
    }

    /**
     * 创建标签
     * @param string $name
     * @param null $mp_info
     * @return array|\EasyWeChat\Kernel\Support\Collection|mixed|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public static function tagCreate($name = '', $mp_info = null)
    {
        $app = self::getApp($mp_info);
        $response = $app->user_tag->create($name);
        if(!empty($response['errcode'])){
            return $response['errmsg'];
        }
        return $response;
    }

    /**
     * 获取已创建列表
     * @param null $mp_info
     * @return array|\EasyWeChat\Kernel\Support\Collection|mixed|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public static function tagList($mp_info = null)
    {
        $app = self::getApp($mp_info);
        $response = $app->user_tag->list();
        if(!empty($response['errcode'])){
            return $response['errmsg'];
        }
        return $response;
    }

    /**
     * 获取公众号tag字典数据 [tag_id => name]
     * @param null $mp_info
     * @param bool $zero_index
     * @return array
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public static function getTagidToName($mp_info = null, $zero_index = false)
    {
        $list = TagM::where('mp_id', $mp_info['id'])
            ->column('name', 'tag_id');
        $zero_index && $list = [0 => '全部'] + $list;
        return  $list;
    }
}